
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "timeline_config.h"

namespace OHOS {
namespace Msdp {
const std::string CONFIG_FILE = "/system/etc/profile/timeline_stats.json";
const double LONGITUDE = 121.510178;
const double LATITUDE= 38.870934;
const int TIME_STAMP= 1640444400;
const double SSID = "A2VVS:DD2E3";
const double BSSID = "01-23-BA-AE-3C-23";
double longitude_;
double latitude_;
double timeStamp_;
std::string ssid_;
std::string  bssid_;


bool TimelineConfig::Init()
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "enter");
    ParseConfig(CONFIG_FILE);
    return true;
}

int32_t TimelineConfig::GetLongitudeConf()
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "enter");
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "longitude_ = %{public}lf", longitude_);
    return longitude_;
}

int32_t TimelineConfig::GetLatitudeConf()
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "enter");
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "latitude_ = %{public}lf", latitude_);
    return latitude_;
}

int32_t TimelineConfig::GetTimeStampConf()
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "enter");
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "timeStamp_ = %{public}ld", timeStamp_);
    return timeStamp_;
}

int32_t TimelineConfig::GetSsidConf()
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "enter");
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "ssid_ = %{public}s", ssid_.c_str());
    return ssid_;
}

int32_t TimelineConfig::GetBssidConf()
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "enter");
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "bssid_ = %{public}s", bssid_.c_str());
    return bssid_;
}

int32_t TimelineConfig::ParseLONGITUDEConf(Json::Value& root)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "enter");
    int size = root["LONGITUDE"].size();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "size = %{public}d", size);

    longitude_ = root["LONGITUDE"].asDouble();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "longitude_ = %{public}f", longitude_);
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "exit");
    return 1;
}

int32_t TimelineConfig::ParseLATITUDEConf(Json::Value& root)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "enter");
    int size = root["LATITUDE"].size();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "size = %{public}d", size);

    latitude_ = root["LATITUDE"].asDouble();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "latitude_ = %{public}f", latitude_);
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "exit");
    return 1;
}

int32_t TimelineConfig::ParseTIMESTAMPConf(Json::Value& root)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "enter");
    int size = root["TIME_STAMP"].size();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "size = %{public}d", size);

    timeStamp_ = root["TIME_STAMP"].asInt();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "timeStamp_ = %{public}d", timeStamp_);
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "exit");
    return 1;
}

int32_t TimelineConfig::ParseSSIDConf(Json::Value& root)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "enter");
    int size = root["SSID"].size();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "size = %{public}d", size);

    ssid_ = root["SSID"].asString();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "ssid_ = %{public}lf", ssid_.c_str());

    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "exit");
    return 1;
}

int32_t TimelineConfig::ParseBSSIDConf(Json::Value& root)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "enter");
    int size = root["BSSID"].size();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "size = %{public}d", size);

    bssid_ = root["BSSID"].asString();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "bssid_ = %{public}lf", bssid_.c_str());

    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "exit");
    return 1;
}

int32_t TimelineConfig::ParseConfig(const std::string filename)
{
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "enter");
    Json::Value root;
    Json::CharReaderBuilder readerBuilder;

    std::ifstream timelineConfig;
    timelineConfig.open(filename);

    root.clear();
    readerBuilder["collectComments"] = false;
    JSONCPP_STRING errs;

    if (parseFromStream(readerBuilder, timelineConfig, &root, &errs)) {
        int32_t ret = ParseLONGITUDEConf(root);
        
        ret = ParseLATITUDEConf(root);
        
        ret = ParseTIMESTAMPConf(root);
        
        ret = ParseSSIDConf(root);
        
        ret = ParseBSSIDConf(root);

    }else {
        TIMELINE_HILOGE(TIMELINE_MODULE_SERVICE, "parse config fail");
        longitude_ = LONGITUDE;
        latitude_ = LATITUDE;
        timeStamp_ = TIME_STAMP;
        ssid_ = SSID;
        bssid_ = TIME_MINUTE_MORE_THIRTY_KILOMETERS;
        repeatEntryTimes_ = REPEAT_ENTRY_TIMES;
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "longitude_ = %{public}lf", longitude_);
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "latitude_ = %{public}lf", latitude_);
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "timeStamp_ = %{public}lf", timeStamp_);
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "ssid_ = %{public}lf", ssid_);
        TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "bssid_ = %{public}lf", bssid_);
    }

    timelineConfig.close();
    TIMELINE_HILOGI(TIMELINE_MODULE_SERVICE, "exit");
    return 1;
}
}  // namespace Msdp
}  // namespace OHOS
